//
//  LoginView.swift
//  FastDevelop
//
//  Created by IOS1 on 2025/6/27.
//

import UIKit

class LoginView: UIView {

    var forgetBlock: (() -> Void)?
    var registerBlock: (() -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubviews([
            accountView,
            pwdView,
            setupBtn
        ])
        accountView.addSubviews([
            accountImageV,
            accountTextF
        ])
        pwdView.addSubviews([
            pwdImageV,
            pwdTextF,
            showBtn
        ])
        
        accountView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(42)
            make.top.equalToSuperview()
        }
        accountImageV.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.leading.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        accountTextF.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.leading.equalToSuperview().offset(55)
            make.trailing.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
        }
        pwdView.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(30)
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(42)
            make.top.equalTo(accountView.snp.bottom).offset(32)
        }
        pwdImageV.snp.makeConstraints { make in
            make.width.height.equalTo(24)
            make.leading.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        pwdTextF.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.leading.equalToSuperview().offset(55)
            make.trailing.equalToSuperview().offset(-55)
            make.centerY.equalToSuperview()
        }
        showBtn.snp.makeConstraints { make in
            make.height.width.equalTo(24)
            make.trailing.equalToSuperview().offset(-13)
            make.centerY.equalToSuperview()
        }
        setupBtn.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-30)
            make.height.equalTo(22)
            make.top.equalTo(pwdView.snp.bottom).offset(30)
        }
    }
    
    @objc private func clickSetupButton(){
        registerBlock?()
    }
    
    @objc private func clickShowPwd(){
        
        showBtn.isSelected = !showBtn.isSelected
        pwdTextF.isSecureTextEntry = !showBtn.isSelected
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var accountView: ShadowView = {
        let v = ShadowView()
        v.cornerRadius = 8
        v.shadowColor = UIColor(hexString: "#333333", alpha: 0.5)
        v.shadowOpacity = 0.5
        v.shadowOffset = CGSize(width: 0, height: 2)
        v.shadowRadius = 5
        v.backgroundColor = .white
        return v
    }()
    private lazy var pwdView: ShadowView = {
        let v = ShadowView()
        v.cornerRadius = 8
        v.shadowColor = UIColor(hexString: "#333333", alpha: 0.5)
        v.shadowOpacity = 0.5
        v.shadowOffset = CGSize(width: 0, height: 2)
        v.shadowRadius = 5
        v.backgroundColor = .white
        return v
    }()
    private lazy var accountImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "login_account")
        return v
    }()
    lazy var accountTextF: UITextField = {
        let v = UITextField()
        v.placeholder = "Kontonummer"
        v.textColor = .black
        v.font = UIFont.systemFont(ofSize: 16)
        return v
    }()
    private lazy var pwdImageV: UIImageView = {
        let v = UIImageView()
        v.contentMode = .scaleAspectFill
        v.image = UIImage(named: "login_pwd")
        return v
    }()
    lazy var pwdTextF: UITextField = {
        let v = UITextField()
        v.placeholder = "Passwort: 6-16 Zeichen"
        v.textColor = .black
        v.font = UIFont.systemFont(ofSize: 16)
        v.isSecureTextEntry = true
        v.backgroundColor = .clear
        return v
    }()
    private lazy var showBtn: UIButton = {
        let v = UIButton(type: .custom)
        v.setImage(UIImage(named: "login_hidden"), for: .normal)
        v.setImage(UIImage(named: "login_show"), for: .selected)
        v.addTarget(self, action: #selector(clickShowPwd), for: .touchUpInside)
        return v
    }()
    private lazy var setupBtn: UIButton = {
        let v = UIButton(type: .custom)
        v.setTitle("registrieren", for: .normal)
        v.setTitleColor(UIColor(hexString: "#0E80FD"), for: .normal)
        v.titleLabel?.font = UIFont.systemFont(ofSize: 12)
        v.addTarget(self, action: #selector(clickSetupButton), for: .touchUpInside)
        return v
    }()
}

